package com.myself.ssoserver.controller

import com.myself.common.page.Page
import com.myself.common.page.PageResult
import com.myself.ssoserver.dto.LotteryDTO
import com.myself.ssoserver.entity.TriLottery
import com.myself.ssoserver.entity.User
import com.myself.ssoserver.repository.TriVersionRepository
import com.myself.ssoserver.service.TriLotteryService
import org.ktorm.dsl.and
import org.ktorm.dsl.eq
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/lottery")
class LotteryController(private val lotteryService: TriLotteryService , private val triVersionRepository: TriVersionRepository) {



    @PostMapping("/buyTriReCord")
    fun buyTriReCord(@RequestBody triLottery: LotteryDTO) {
        lotteryService.validateLottery(triLottery)
        lotteryService.buyTriReCord(triLottery.toEntity())
    }


    @PostMapping("/betHistory")
    fun betHistory(@RequestBody page: Page): PageResult<TriLottery> {
        val authentication = SecurityContextHolder.getContext().authentication
        val user: User = authentication.principal as User;
        val currentTriVersion = triVersionRepository.findCurrent()
        return lotteryService.findPageListBy(page){ (it.userId eq user.id) and (it.versionId eq currentTriVersion.id) }
    }

}
